/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ortus;

import java.util.List;
import java.util.Random;
import sagex.api.MediaFileAPI;

/**
 *
 * @author jphipps
 */
public class Identity extends ortusvars {
    String ClientName;
    int CurrentUser;

   public Identity(String MAC) {
		ortus.api.DebugLog(INFO, "Ortus Identity: Loading");

		if (ortus.api.GetSageProperty("ortus/clientname", null) == null) {
			Random ran = new Random();
			int rnum = ran.nextInt();
			ClientName = MAC + "-" + String.valueOf(rnum);
			ortus.api.SetSageProperty("ortus/clientname", ClientName);
			ortus.api.DebugLog(TRACE2, "Ortus: Identity generated client name: " + ClientName);
		} else {
			ClientName = ortus.api.GetSageProperty("ortus/clientname", null);
			ortus.api.DebugLog(TRACE2, "Ortus: Identity found " + ClientName + " in the properties file");
		}

		CurrentUser = Integer.parseInt(ortus.api.GetSageProperty("ortus/user/current", "0"));

		ortus.api.DebugLog(INFO, "Ortus: Identity loaded for " + ClientName);
	}

    public String GetClientName() {
        return ClientName;
    }

    public void SetClientName(String clientname) {
        ClientName = clientname;
        ortus.api.GetSageProperty("ortus/clientname", ClientName);
    }

    public int GetCurrentUser() {
        return CurrentUser;
    }
    
    public Object GetCurrentUserName() {
        if ( Ortus.getInstance().modules.get("db") != null ) {
            return ortus.api.executeSQLQuery("select username from sage.user where userid = " + CurrentUser).get(0);
        } else
            return null;
    }

    public Object GetUserName(int userid) {
        return ortus.api.executeSQLQuery("select username from sage.user where userid = " + userid).get(0);
    }
    
    public Object GetUserThumb(int userid) {
        return ortus.api.executeSQLQuery("select userthumb from sage.user where userid = " + userid).get(0);
    }

    public void SetCurrentUser(int userid) {
        CurrentUser = userid;
        ortus.api.GetSageProperty("ortus/user/current", String.valueOf(CurrentUser));
    }

    public List<Object> GetUsers() {
        return ortus.api.executeSQLQuery("select userid from sage.user");
    }

    public boolean IsUserWatched(Object mediafile) {
        List<Object> result = ortus.api.executeSQLQuery("select watched from sage.usermedia where userid = " + CurrentUser + " and mediaid = " + MediaFileAPI.GetMediaFileID(mediafile));
        ortus.api.DebugLog(TRACE2, "IsUserWatched: " + result);
        if ( result.get(0).equals("1"))
            return true;
        else
            return false;
    }

    public long GetUserWatchPosition(Object mediafile) {
        List<Object> result = ortus.api.executeSQLQuery("select watchedposition from sage.usermedia where userid = " + CurrentUser + " and mediaid = " + MediaFileAPI.GetMediaFileID(mediafile));
        ortus.api.DebugLog(TRACE2, "GetUserWatchedPosition: " + result);
        return Long.parseLong((String)result.get(0));
    }

 }
